package com.hckjsec.official.controller;

import java.util.List;

import com.hckjsec.common.utils.DateUtils;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import com.hckjsec.common.utils.uuid.IdUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.official.domain.OfficialConfig;
import com.hckjsec.official.service.IOfficialConfigService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 公司网站主Controller
 * 
 * @author cyt
 * @date 2021-12-02
 */
@RestController
@RequestMapping("/official/config")
public class OfficialConfigController extends BaseController
{
    @Autowired
    private IOfficialConfigService officialConfigService;

    /**
     * 获取公司网站主详细信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo()
    {
        OfficialConfig findOfficialConfig = new OfficialConfig();
        OfficialConfig officialConfig = new OfficialConfig();
        officialConfig.setDeptId(SecurityUtils.getDeptId());
        List<OfficialConfig> officialConfigList = officialConfigService.selectOfficialConfigList(officialConfig);
        if (!StringUtils.isEmpty(officialConfigList)){
            findOfficialConfig = officialConfigList.get(0);
        }
        return AjaxResult.success(findOfficialConfig);
    }

    /**
     * 新增公司网站主
     */
    @Log(title = "公司网站主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OfficialConfig officialConfig)
    {
        int rows = 0;
        OfficialConfig findOfficialConfig = new OfficialConfig();
        findOfficialConfig.setDeptId(SecurityUtils.getDeptId());
        List<OfficialConfig> officialConfigList = officialConfigService.selectOfficialConfigList(findOfficialConfig);
        if (!StringUtils.isEmpty(officialConfigList)){
            officialConfig.setUpdateBy(SecurityUtils.getUsername());
            rows =  officialConfigService.updateOfficialConfig(officialConfig);
        }else{
            officialConfig.setId(IdUtils.simpleUUID());
            officialConfig.setDeptId(SecurityUtils.getDeptId());
            officialConfig.setCreateBy(SecurityUtils.getUsername());
            rows =  officialConfigService.insertOfficialConfig(officialConfig);
        }
        return toAjax(rows);
    }
}
